package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.AreaListAdapter;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.AreaListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author programming
 *@date 2017/5/31 0031
 */
public class SelectCityActivity extends BaseSecondActivity {


    @BindView(R.id.address_head)
    ExpandHeadView mAddressHead;
    @BindView(R.id.lv_address)
    ListView mLvAddress;
    public AreaListBean mAreaListBean;
    public String mCityName;


    @Override
    protected void initView() {
        initData();
        initListener();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_address;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mAddressHead;
    }


    protected void initListener() {

        mLvAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaListBean.ListAreaBean listAreaBean = mAreaListBean.getListArea().get(position);
                mCityName = listAreaBean.getName();
                String listAreaBeanId = listAreaBean.getId();
                String parentId = listAreaBean.getId();
                Intent intent = new Intent(UIUtils.getContext(), SelectAreaActivity.class);
                intent.putExtra("parentIds", parentId);
                intent.putExtra("province", getIntent().getStringExtra("province"));
                intent.putExtra("cityName", mCityName);
                SPUtils.saveString("areaid", listAreaBeanId);
                startActivity(intent);
                finish();
            }
        });

    }


    protected void initData() {
        String parentId = getIntent().getStringExtra("parentId");
        try {
            OkHttpUtils.post().url(Constants.CITYURL).addParams("type", "2")
                    .addParams("parentId", parentId)
                    .addParams("sign", Constants.SIGN)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            ALog.e("请求数据失败,给出提示用户检查网络");
                            MyToast.show("获取数据失败,请检查网络...");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Gson gson = new Gson();
                            mAreaListBean = gson.fromJson(response, AreaListBean.class);
                            mLvAddress.setAdapter(new AreaListAdapter(mAreaListBean.getListArea()));
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
